<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

//header and nav
$this->controller->echo_header();

//make javascript array from sections
$javascript_array_sections = 'var sections = new Array(';
$first = true;
foreach($this->sections as $section){		
	if($first){
		$first = false;
	}else{
		$javascript_array_sections .= ',';
	}
	$javascript_array_sections .= "'".$section->id."'";
}	
$javascript_array_sections .= ');';
	
?>
<script language="javascript" type="text/javascript">

<?php echo $javascript_array_sections."\n"; ?>

function select_all(usergroup_id, select_all_id){
	action = document.getElementById(select_all_id).checked;	
	for (i = 0; i < sections.length; i++){
		box_id = sections[i]+'__'+usergroup_id;
		hidden_id = sections[i]+'__'+usergroup_id+'__hidden';
		if(action==true){
			document.getElementById(box_id).checked = true;
			document.getElementById(hidden_id).value = hidden_id+'__1';
		}else{
			document.getElementById(box_id).checked = false;
			document.getElementById(hidden_id).value = hidden_id+'__';
		}
	}	
}

function toggle_right(hidden_field_id){
	field = document.getElementById(hidden_field_id);
	if(field.value==hidden_field_id+'__1'){
		field.value = hidden_field_id+'__';
	}else{
		field.value = hidden_field_id+'__1';
	}
}

function check_limitstart(){
	ori_search = '<?php echo $this->search;?>';	
	new_search = document.getElementById('search').value;	
	if(ori_search!=new_search){
		document.adminForm.limitstart_needs_reset.value = 1;
	}else{
		document.adminForm.limitstart_needs_reset.value = 0;
	}	
	return true;
}

</script>
<form name="adminForm" method="post" action="">
	<input type="hidden" name="option" value="com_frontenduseraccess" />
	<input type="hidden" name="task" value="" />	
	<input type="hidden" name="limitstart_needs_reset" value="" />	
	<?php echo JHTML::_( 'form.token' ); ?>	
<table id="fua_subheader">
	<tr>
		<td>
			<?php echo '<p>'.JText::_('SECTIONS_INFO').'.</p>';
			//legend and message if reverse access	
			$this->controller->reverse_access_warning('sections_reverse_access');
			
			//message in free version that these restrictions will not work in free version
			$this->controller->not_in_free_version();
			
			//message if section access is not activated
			if($this->controller->fua_config['sections_active']==false){				
				echo '<div style="color: red; text-align: left;">'.JText::_('NO_SECTIONS_ACTIVE').'. <a href="index2.php?option=com_frontenduseraccess&view=config&tab=section_access">'.JText::_('ACTIVATE_IN_CONFIG').'</a><br/><br/></div>';
			}	
			
			echo JText::_('FILTER'); ?>:
			<input type="text" name="search" id="search" value="<?php echo $this->search;?>" class="text_area"  onchange="check_limitstart();" />
			<button onclick="check_limitstart();this.form.submit();"><?php echo JText::_('GO'); ?></button>
			&nbsp;<button onclick="document.getElementById('search').value='';this.form.submit();"><?php echo JText::_('RESET'); ?></button>
		</td>
		<td id="td_usergroup_selector">
			<?php echo $this->controller->usergroup_selector(); ?>
		</td>
	</tr>
</table>
<table class="adminlist">
	<tr>		
		<th align="left">&nbsp;
						
		</th>
		<?php			
			$this->controller->loop_usergroups($this->fua_usergroups);			
		?>			
	</tr>
		
	<?php
							
		$k = 1;		
		
		//row with select_all checkboxes
		echo '<tr class="row1">';
		echo '<td>'.JText::_('SELECTALL').'</td>';
		foreach($this->fua_usergroups as $fua_usergroup){
			echo '<td style="text-align:center;"><input type="checkbox" name="checkall[]" value="" id="checkall_'.$fua_usergroup->id.'" onclick="select_all('.$fua_usergroup->id.',this.id);" /></td>';
		}
		echo '</tr>';
			
		$counter = 0;		
		foreach($this->sections as $section){						
			echo '<tr class="row'.$k.'"><td>'.$section->title.'</td>';			
			foreach($this->fua_usergroups as $fua_usergroup){
				$checked = '';
				$checked_hidden = '';
				if (in_array($section->id.'__'.$fua_usergroup->id, $this->access_sections)) {
					$checked = 'checked="checked"';
					$checked_hidden = '1';
				}
				echo '<td style="text-align:center;"><input type="hidden" name="section_access_hidden[]" id="'.$section->id.'__'.$fua_usergroup->id.'__hidden" value="'.$section->id.'__'.$fua_usergroup->id.'__hidden__'.$checked_hidden.'" /><input type="checkbox" name="sectionAccess[]" id="'.$section->id.'__'.$fua_usergroup->id.'" onclick="toggle_right(\''.$section->id.'__'.$fua_usergroup->id.'__hidden\');" value="'.$section->id.'__'.$fua_usergroup->id.'" '.$checked.' /></td>';
			}
			echo '</tr>';
			if($k==1){
				$k = 0;
			}else{
				$k = 1;
			}	
			if($counter==7){
				echo '<tr><th>&nbsp;</th>';	
				$this->controller->loop_usergroups($this->fua_usergroups);
				echo '</tr>';
				$counter = 0;
			}
			$counter = $counter+1;								
		}	
	echo '<table class="adminlist"><tfoot><tr><td>'.$this->pagination->getListFooter().'</td></tr></tfoot></table>';
	?>			
</table>
</form>
<?php

$this->controller->display_footer();

?>